<template>
  <el-dialog
    v-el-drag-dialog
    title="退单/确认"
    :visible.sync="visible"
    width="800px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :model="temp" :rules="rules" label-position="right" label-width="100px" style="margin: 0 50px;">
      <el-form-item label="客户名称">
        <el-input v-model="temp.csmName" disabled />
      </el-form-item>
      <el-form-item label="客户电话">
        <el-input v-model="temp.contactPhone" disabled />
      </el-form-item>
      <el-form-item label="客户地址" class="block">
        <el-input v-model="temp.contactAddress" disabled />
      </el-form-item>
      <el-form-item label="退单人" prop="userName">
        <el-input v-model="temp.userName" disabled />
      </el-form-item>
      <el-form-item label="退单日期" prop="operateDt">
        <el-date-picker
          v-model="temp.operateDt"
          style="width: 100%;"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item class="block" label="退单原因:" prop="marker4">
        <el-checkbox-group v-if="isExist === 1" v-model="checkedTypes">
          <el-checkbox v-for="item in commonKeysInfoList" :key="item.keyId" :value="item.keyId" :label="item.keyName" />
        </el-checkbox-group>
        <el-input v-model="temp.marker4" type="textarea" :rows="3" :maxlength="isExist === 1 ? 100 : 199" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryRepairOrderBack, queryRepairOrderBackParam } from '@/api'
  import moment from 'dayjs'

  export default {
    name: 'back-order',
    data() {
      const validateMarker = (rule, value, callback) => {
        if (this.isExist === 0) {
          if (value === '') callback(new Error('请输入退单原因'))
          else callback()
        } else {
          if (this.checkedTypes.length === 0 && value === '') callback(new Error('请输入退单原因'))
          else callback()
        }
      }
      return {
        visible: false,
        isExist: 0,
        commonKeysInfoList: [],
        checkedTypes: [],
        temp: {
          userName: '',
          operateDt: moment(),
          marker4: ''
        },
        rules: {
          userName: [{ required: true, message: '请输入退单人', trigger: 'change' }],
          operateDt: [{ required: true, message: '请选择退单日期', trigger: 'change' }],
          marker4: [{ validator: validateMarker, trigger: 'blur' }]
        }
      }
    },
    methods: {
      open(temp) {
        this.getDefaultData()
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
          }
        })
      },
      async getDefaultData() {
        const res = await queryRepairOrderBackParam()
        this.isExist = Number(res.data.isExist)
        this.commonKeysInfoList = JSON.parse(res.data.commonKeysInfoList)
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            repairCode: this.temp.repairCode,
            userId: this.temp.userId,
            userName: this.temp.userName,
            operateDt: moment(this.temp.operateDt).format()
          }
          if (this.isExist === 0) {
            params.marker4 = this.temp.remark
          } else {
            let reason = ''
            if (this.checkedTypes.length > 0) {
              this.checkedTypes.forEach(item => {
                reason += `${item}，`
              })
            }
            const reason2 = this.temp.remark
            if (reason !== '' && reason2 === '') {
              params.marker4 = reason.substring(0, reason.length - 1)
            } else {
              params.marker4 = reason + reason2
            }
          }
          if (params.marker4.length > 200) {
            this.$message.warning('退单原因字数最长为200')
            return
          }
          this.submit(params)
        })
      },
      async submit(params) {
        await queryRepairOrderBack(params)
        this.resolve(true)
        this.visible = false
        this.$notify({
          title: '提示',
          message: '退单成功!',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.isExist = 0
        this.commonKeysInfoList = []
        this.temp = {
          userName: '',
          operateDt: moment(),
          marker4: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  & /deep/ .el-form-item {
    width: 48%;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
</style>
